package com.example.zheng.qq.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zheng.qq.R;
import com.example.zheng.qq.controller.CreateTable;
import com.example.zheng.qq.controller.ProgressTask;
import com.example.zheng.qq.entity.UseName;
import com.example.zheng.qq.util.Util;
import com.example.zheng.qq.view.Circle1;
import com.example.zheng.qq.view.MyGifView;


import pl.droidsonroids.gif.GifImageView;


/**
 * Created by zheng on 17-4-25.
 */
public class Login extends Activity{
    private ImageView mQQ;
    private EditText mName;
    private EditText mPass;
    private Button mLogin;
    private CheckBox mRemember;
    private SharedPreferences mShared;
    private SharedPreferences.Editor mEditor;
    private ProgressBar mProgress;
    private WindowManager.LayoutParams mParams;
    private LinearLayout mLinearLayout;
    private WindowManager mWindow;
    private TextView mText;
    private ProgressTask progressTask;
    private SQLiteDatabase mDb;
    private Boolean isRemember;
    private GifImageView gifImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        gifImageView=(GifImageView)findViewById(R.id.gifImage);

        Log.d("dd",R.drawable.qqbackground+"");
        mDb=Util.Db;
        mQQ=(ImageView)findViewById(R.id.qq);
        mName=(EditText)findViewById(R.id.usename);
        mPass=(EditText)findViewById(R.id.passw);
        mRemember=(CheckBox)findViewById(R.id.remember);
        mShared=getSharedPreferences("data",MODE_PRIVATE);
        mEditor=mShared.edit();
       /* mPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                mPass.setBackgroundColor(Color.RED);
            }
        });*/



    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onStart() {


        isRemember=mShared.getBoolean("isRemember",false);



        if(isRemember){
            String name= mShared.getString("name","");
            String pass= mShared.getString("pass","");
            mName.setText(name);
            mPass.setText(pass);
            mRemember.setChecked(true);
        }else{
            mName.setText("");
            mPass.setText("");
            mRemember.setChecked(false);
        }


        String name=mName.getText().toString();
        Util.SqlPictureId(name,mQQ);
        super.onStart();
    }

    /**
     * 给登录按钮绑定点击事件，并判断用户和密码是否匹配，是通过在查询数据库中是否存在该用户
     * 并判断是否记住密码，如果是记住密码，则把用户的信息和记住密码的变量放在本地而不是数据库，减少不必要的对数据库不停的操作
     * @param v
     */
    public void doclick(View v){
        switch (v.getId()){
            case R.id.login:
                String name=mName.getText().toString();
                Toast.makeText(this,name,Toast.LENGTH_SHORT).show();
                String pass=mPass.getText().toString();
                Cursor data=mDb.rawQuery("select * from user where uname=? and pass=?",new String[]{name,pass});
                if(data.moveToNext()){
                    if(mRemember.isChecked()){
                        mEditor.putString("name",name);
                        mEditor.putString("pass",pass);
                        mEditor.putBoolean("isRemember",true);
                    }else{
                        mEditor.clear();
                    }
                    mEditor.apply();
                    createFloatView();
                    UseName useName=new UseName();
                    useName.setHeadPicture(data.getString(2));
                    useName.setName(name);
                    useName.setPass(pass);

//                    Toast.makeText(this,"结束",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(this,MainActivity.class);
                    intent.putExtra("user",useName);
                    startActivity(intent);
                    data.close();
                    finish();
                }

                break;
            case R.id.register:
                mEditor.putBoolean("isRemember",false);
                mEditor.apply();

                Toast.makeText(this,"别点我",Toast.LENGTH_SHORT).show();
                openActivity(RegisterLayout.class);
                break;


        }
    }


    private void openActivity(Class mainActivityClass) {
        Intent intent=new Intent(this,mainActivityClass);
        startActivity(intent);
        overridePendingTransition(R.anim.pull_in_right,R.anim.pull_out_left);
    }

    //悬浮窗口的实现
    private void createFloatView() {
        mWindow= (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        mParams=new WindowManager.LayoutParams();
        mParams.type= WindowManager.LayoutParams.TYPE_PHONE;
        mParams.format= PixelFormat.RGBA_8888;
        mParams.flags= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mParams.gravity= Gravity.TOP|Gravity.LEFT;
        mParams.x=0;
        mParams.y=0;
//        mParams.format=1;
        mParams.alpha=0.5f;
        mParams.width=WindowManager.LayoutParams.MATCH_PARENT;
        mParams.height=WindowManager.LayoutParams.MATCH_PARENT;
        LayoutInflater inflater=LayoutInflater.from(getApplication());
        mLinearLayout= (LinearLayout) inflater.inflate(R.layout.progress_layout,null);
        mLinearLayout.setFocusable(true);
        mWindow.addView(mLinearLayout,mParams);
        mText=(TextView)mLinearLayout.findViewById(R.id.textLogin);
        mProgress=(ProgressBar)mLinearLayout.findViewById(R.id.progressBar);
        progressTask=new ProgressTask(mLinearLayout,mWindow);
        progressTask.execute(0);


    }


}
